package com.hjl.spring6.tx.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

/**
 * @author ServivE
 * @version 0.1
 * @className BookDaoImpl
 * @description
 * @date 2024/8/7 20:49
 * @since jdk11
 */
@Repository
public class BookDaoImpl implements BookDao{
    @Autowired
    private JdbcTemplate jdbcTemplate;
    //根据图书id查询图书价格
    @Override
    public Integer selectPriceByBookId(Integer bookId) {
        String sql = "select price from t_book where book_id = ?";
        Integer price = jdbcTemplate.queryForObject(sql, Integer.class, bookId);
        return price;
    }

    @Override
    //更新图书表库存量-1
    public void updateStock(Integer bookId) {
        String sql = "update t_book set stock=stock-1 where book_id = ?";
        jdbcTemplate.update(sql, bookId);

    }
    @Override
    //更新用户表用户余额-图书价格
    public void updateUserBalance(Integer userId, Integer price) {
        String sql = "update t_user set balance = balance - ? where user_id = ?";
        jdbcTemplate.update(sql, price, userId);
    }
}
